package com.examples.spider;

import org.apache.commons.lang3.StringUtils;
import us.codecraft.webmagic.utils.FilePersistentBase;

import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Author : feilong.gao
 * email : feilong.gao.thebeastshop.com
 * date : 2021/2/18
 */
public class ConvertGitBook2Txt extends FilePersistentBase {

    private static String CHAPTER_FILES = "chapter";
    private static String CHAPTER_URL_SUFFIX = ".html";
    private static String path = "D:\\小说集\\种菜骷髅的异域开荒\\www.ibiquge.la";
    private static String gitBookSummary = "SUMMARY.md";
    private static String outFileName = "种菜骷髅的异域开荒.txt";

    private void gitBook2Markdown() {

        String outFile = path + FilePersistentBase.PATH_SEPERATOR + outFileName;
        String inSummaryFile = path + FilePersistentBase.PATH_SEPERATOR + gitBookSummary;
        try {
            BufferedReader summaryReader = new BufferedReader(new FileReader(getFile(inSummaryFile)));
            PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(getFile(outFile), false), "UTF-8"));

            while (true) {
                String title = summaryReader.readLine();
                if (StringUtils.isBlank(title)) {
                    summaryReader.close();
                    printWriter.println();
                    printWriter.close();
                    break;         // 结束读取
                }
                String chapterPath = parseString(title, false);
                if (chapterPath.contains(CHAPTER_FILES)) {
                    BufferedReader chapterReader;
                    try {
                        String chapterFile = path + FilePersistentBase.PATH_SEPERATOR + chapterPath;
                        chapterReader = new BufferedReader(new FileReader(getFile(chapterFile)));
                    }catch (FileNotFoundException f){
                        break;
                    }
                    String chapterLine = "";
                    Integer blankNum = 0;
                    while (true) {
                        chapterLine = chapterReader.readLine();

                        if (StringUtils.isBlank(chapterLine)) {
                            blankNum += 1;
                            if (blankNum > 1) {
                                chapterReader.close();
                                break;
                            }
                        } else {
                            if (chapterLine.contains(CHAPTER_URL_SUFFIX)) {
                                printWriter.println(parseString(chapterLine, true));
                            } else {
                                printWriter.println(chapterLine.replace("　　","\n"));
                            }
                        }

                    }
                    printWriter.println();
                } else {
                    printWriter.println(parseString(title, true));
                }

            }


        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        ConvertGitBook2Txt convert = new ConvertGitBook2Txt();
        convert.gitBook2Markdown();

//        String aa = "## [ 252、鲁迅先生（第二更）](https://www.ddyueshu.com/6_6394/391641623.html)";
//        System.out.println(ConvertGitBook2Txt.parseString(aa,true));
    }


    private static String parseString(String source, Boolean isTitle) {
        String resultStr = "";
        String pattern = null;
        if (isTitle) {
            pattern = "(?<=\\[).*?(?=\\])";  //括号内内容为要提取的内容
            Matcher m = Pattern.compile(pattern).matcher(source);
            resultStr = m.find() ? m.group().trim() : "";
            if (Pattern.compile("^[0-9]*、").matcher(resultStr).find()) {
                resultStr = "第" + resultStr.replace("、", "章 ");
            }


        } else {
            pattern = "(?<=\\().*?(?=\\))";  //括号内内容为要提取的内容
            Matcher m = Pattern.compile(pattern).matcher(source);
            resultStr = m.find() ? m.group() : "";
        }
        return resultStr;
    }


}
